package net.viralpatel.struts2;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.opensymphony.xwork2.ActionSupport;

public class OrderAction extends ActionSupport {
	private String startDate;
	private String endDate;
	private String startTime;
	private String endTime;
	private String type;
	private String floor;
	private String number;
	private JDBCQuery jdbc;
	private List<Date> dates = new ArrayList<Date>();
	
	private String yoyakuID = "";
	private String roomID = "";
	
	private String userID = "longnt";
	
	private String message;
	private float cost;

	public String execute() {
		System.out.println("type: " + type + "/ floor: " + floor + " / number: " + number
		+ "starDate : " + startDate + " / endDate" + endDate + " / startTime: "
		
		+ startTime + " / endTime: " + endTime);
		
		char c = type.charAt(0);
		
		System.out.println(c);
		
		String query = "Select price from room where roomType2 = '" + c + "'";
		System.out.println(query);
		int price = 0;
		try {
			jdbc = new JDBCQuery();
			ResultSet result = jdbc.getData(query);
			while(result.next()){
				price = result.getInt("price");
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*Tinh tien*/
		String[] hmStart = startTime.split(":");
		String[] hmEnd = endTime.split(":");
		int minuteNumber = (Integer.parseInt(hmEnd[0]) - Integer.parseInt(hmStart[0])) * 60 
							+ (Integer.parseInt(hmEnd[1]) - Integer.parseInt(hmStart[1]));	 
		cost = (minuteNumber / 30) * price;
		switch (c) {
		case 'A':
//			cost = (minuteNumber / 30) * 30000;
			roomID = "1" + type + floor + number; 
			break;
		case 'B':
//			cost = (minuteNumber / 30) * 20000;
			roomID = "1" + type + floor + number; 
			break;
		case 'C':
//			cost = (minuteNumber / 30) * 20000;
			roomID = "1" + type + floor +  number; 
			break;
		case 'D':
//			cost = (minuteNumber / 30) * 15000;
			roomID = "1" + type + number; 
			break;
		case 'H':
		case 'I':
		case 'J':
			roomID = "2" + type + floor + number;
//			cost = (minuteNumber / 30) * 10000;
			break;
		default:
			break;
		}
		listDate(startDate, endDate);
		if(dates.size() == 0){
			setMessage("error1");
			return ActionSupport.SUCCESS;
		}
		try {
			DateFormat formatter;
			formatter = new SimpleDateFormat("dd-MM-yyyy");
//			jdbc = new JDBCQuery();
			String d_startTime = "";
			String d_endTime = "";
			boolean isUpdate;
			for(int j = 0; j < dates.size(); j ++){
				isUpdate = true;
				Date lDate = dates.get(j);
				String ds = formatter.format(lDate);
				yoyakuID = roomID + "_" + ds;		
				query = "Select startTime, endTime from  yoyaku where day = '" + ds + "' AND roomID = '" + roomID + "'";
				System.out.println(query);
				ResultSet res = jdbc.getData(query);
				while(res.next()){
					d_startTime = res.getString("startTime");
					d_endTime = res.getString("endTime");	
					System.out.println(d_startTime + " / " + d_endTime);
					if(!d_startTime.equals("")){
						if(isTimeBetween(d_startTime, d_endTime, startTime)
						  || isTimeBetween(startTime, endTime, d_startTime)){
							isUpdate = false;
							System.out.println("Error........................");
							setMessage("error");
							return ActionSupport.SUCCESS;
						}
					}
				}		
				String sql = "Insert into yoyaku value('0', '" + yoyakuID +"', '" + userID + "', '" + roomID
							  + "', '" + ds + "', '" + startTime + "', '" + endTime + "', '0','0','','" + cost + "')";
				System.out.println(sql);
				jdbc.updateData(sql);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				jdbc.closeConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		setMessage("success");
		return ActionSupport.SUCCESS;
	}
	public boolean isTimeBetween(String startTime, String endTime, String seachTime){
		String[] start_times = startTime.split(":");
		int start_h = Integer.parseInt(start_times[0]);
//		int start_m = Integer.parseInt(start_times[1]);
		
		
		String[] end_times = endTime.split(":");
		int end_h = Integer.parseInt(end_times[0]);
//		int end_m = Integer.parseInt(end_times[1]);
		
		String[] seach_times = seachTime.split(":");
		int seach_h = Integer.parseInt(seach_times[0]);
//		int seach_m = Integer.parseInt(seach_times[1]);
		
		if(start_h <= seach_h && seach_h <= end_h){
			return true;
		}
		return false;
	}
	public void listDate(String str_date, String end_date) {
		DateFormat formatter;
		formatter = new SimpleDateFormat("dd-MM-yyyy");
		try {
			Date startDate = (Date) formatter.parse(str_date);
			Date endDate = (Date) formatter.parse(end_date);
			long interval = 24 * 1000 * 60 * 60; // 1 hour in millis
			long endTime = endDate.getTime(); // create your endtime here,
												// possibly using Calendar or
												// Date

			long curTime = startDate.getTime();
			while (curTime <= endTime) {
				Date date = new Date(curTime);
				dates.add(new Date(curTime));
				curTime += interval;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < dates.size(); i++) {
			Date lDate = (Date) dates.get(i);
			
			String ds = formatter.format(lDate);
			System.out.println(" Date is ..." + ds);
		
		}

	}



	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getStartTime() {
		return startTime;
	}
	public void setType(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getNumber() {
		return number;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}
	public void setFloor(String floor) {
		this.floor = floor;
	}
	public String getFloor() {
		return floor;
	}

}
